/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.smarttmt.idu.web.beans;

import com.smarttmt.idu.ejb.domain.Constants;
import com.smarttmt.idu.web.persistence.controller.UsuacontJpaController;
import com.smarttmt.idu.web.persistence.entity.Usuacont;
import com.smarttmt.idu.web.utilities.FacesUtils;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.transaction.UserTransaction;

/**
 *
 * @author Nicolás Rubén Caballero Ortega (helaman58@gmail.com)
 */
@Named(value = "changePasswordForgotBean")
@SessionScoped
public class ChangePasswordForgot implements Serializable {

    private boolean showResetForm = false;
    private String userKey;
    @PersistenceUnit(unitName = "smarttmtiduappPU")
    private EntityManagerFactory emf;
    @Resource
    private UserTransaction utx;
    private UsuacontJpaController usuacontJpaController;
    private Usuacont usuacont;
    private String password;
    private String reTypePassword;

    /**
     * Creates a new instance of ChangePasswordForgot
     */
    public ChangePasswordForgot() {
    }

    public void resetData() {
        this.password = null;
        this.reTypePassword = null;
    }

    @PostConstruct
    public void postConstructorChangePasswordForgot() {
        usuacontJpaController = new UsuacontJpaController(utx, emf);
    }

    public boolean validaKeyContent(String key) {
        return !(key.toUpperCase().contains("SELECT") || key.toUpperCase().contains("UPDATE") || key.toUpperCase().contains("DELETE") || key.toUpperCase().contains("FROM")
                || key.toUpperCase().contains("%")
                || key.toUpperCase().contains("*"));
    }

    public void resetUserWithKey(String key) throws Exception {
        this.userKey = null;
        this.usuacont = usuacontJpaController.getUserByUsconomb(usuacontJpaController.getUserByNombOrEmail(key));
        this.userKey = key;
    }

    public boolean comparePasswordAndReTypePassword() {
        return password.equals(reTypePassword);
    }

    public String changePasswordForgotOperation() {

        if (comparePasswordAndReTypePassword()) {
            try {
                usuacont.setUscopass(password);
                usuacontJpaController.edit(usuacont);
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Se ha reiniciado su contraseña de usuario exitosamente", ""));
                return "index";
            } catch (Exception e) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "No se ha podido restablecer su contraseña de usuario", "Verifique la información suministrada o solicite un nuevo correo"));
                return null;
            }

        }else{
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Las contraseñas deben ser iguales", "Verifique la información suministrada"));
        }

        return null;
    }

    public void loadParametersReset() throws Exception {
        String key = FacesUtils.getSessionParameter(Constants.KEY_PARAMETER);
        if (key != null) {
            if (validaKeyContent(key)) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "No ha sido posible reiniciar la clave de usuario", "Comuniquese con nosotros para resolver el problema"));
            } else {
                resetUserWithKey(key);
            }
        }

        showResetForm = true;
    }

    public boolean isShowResetForm() {
        return showResetForm;
    }

    public void setShowResetForm(boolean showResetForm) {
        this.showResetForm = showResetForm;
    }

    public String getUserKey() {
        return userKey;
    }

    public void setUserKey(String userKey) {
        this.userKey = userKey;
    }

    public EntityManagerFactory getEmf() {
        return emf;
    }

    public void setEmf(EntityManagerFactory emf) {
        this.emf = emf;
    }

    public UserTransaction getUtx() {
        return utx;
    }

    public void setUtx(UserTransaction utx) {
        this.utx = utx;
    }

    public UsuacontJpaController getUsuacontJpaController() {
        return usuacontJpaController;
    }

    public void setUsuacontJpaController(UsuacontJpaController usuacontJpaController) {
        this.usuacontJpaController = usuacontJpaController;
    }

    public Usuacont getUsuacont() {
        return usuacont;
    }

    public void setUsuacont(Usuacont usuacont) {
        this.usuacont = usuacont;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getReTypePassword() {
        return reTypePassword;
    }

    public void setReTypePassword(String reTypePassword) {
        this.reTypePassword = reTypePassword;
    }

}
